package dbSrc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnection {
	private static DbConnection INSTANCE = null;
	private Connection connection = null;
  
	private DbConnection() {
	    connect();
	}
	  
	private void connect() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
			System.err.println("Fehler beim MySQL-Treiber: " + e1.getMessage());
			e1.printStackTrace();
		}
		
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/rate-your-series", "user", "userpass");
		} catch (SQLException e) {
			System.err.println("Fehler beim Verbinden mit der SQL-DB: " + e.getMessage());
			e.printStackTrace();
		}
	}
	  
	private static DbConnection getInstance() {
		INSTANCE = new DbConnection();
		return INSTANCE;
	}
		  
	public static Connection getConnection() {
		DbConnection dbc = getInstance();
		return dbc.connection;
	}


	public static void close() throws SQLException{
		if(INSTANCE != null){
			DbConnection dbc = getInstance();
			dbc.connection.close();
			INSTANCE = null;
		}
	}
}